import { ref, onMounted } from 'vue';
import useService from '@/hooks/useService';
import {getRdsUrl, rdsRequestAsync} from '@/hooks/rdsServices';

export function useWeightUnits() {
    const rdsUrl = getRdsUrl();

    const weightUnits = ref([]);
    const isLoading = ref(false);
    const hasError = ref(false);
    const service = useService({ prefix: 'rds-bc' });

    const initWeightUnits = async () => {
        isLoading.value = true;
        hasError.value = false;

        try {
            const result = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'FetchWeightUnit', '');
            if (!result.HasException) {
                weightUnits.value = result.ObjectData;
            } else {
                hasError.value = true;
                console.error('Failed to fetch weight units:', result.ExceptionMessage);
            }
        } catch (error) {
            hasError.value = true;
            console.error('An unexpected error occurred:', error);
        } finally {
            isLoading.value = false;
        }
    };

    onMounted(async () => {
        await initWeightUnits();
    });

    return {
        weightUnits,
        isLoading,
        hasError,
        initWeightUnits
    };
}
